(function(angular) {
	'use strict';
	angular.module('fs.waf').controller('cda.siteCurrencyCalendar.viewController', viewController);
	viewController.$inject = [ '$scope', 'Constants', 'layoutConfig', 'validatorConfig', 'gridService', 'authService', 'siteCurrencyCalendarService' ];
	function viewController($scope, Constants, layoutConfig, validatorConfig, gridService, authService, siteCurrencyCalendarService) {
		var ctrl = this, siteCurrencyCalendarInfo = $scope.$stateParams.params;
		ctrl.layoutConfig = layoutConfig;
		ctrl.validatorConfig = validatorConfig;
		ctrl.siteCurrencyCalendar = {};
		
		$scope.$watch('viewCalendarYear', function(newValue, oldValue) {
			siteCurrencyCalendarInfo.calendarDay.year = newValue;
			authService.view(siteCurrencyCalendarInfo).then(function(response) {				
				angular.merge(ctrl.siteCurrencyCalendar, response.data);
				$scope.calendarYear = ctrl.siteCurrencyCalendar.calendarDay.year;
			});
		});
		
		ctrl.init = function() {	
			siteCurrencyCalendarService.initOptions($scope, ctrl);
			return authService.view(siteCurrencyCalendarInfo).then(function(response) {
				ctrl.siteCurrencyCalendar = response.data || siteCurrencyCalendarInfo;
				$scope.calendarYear = ctrl.siteCurrencyCalendar.calendarDay.year;
				$scope.viewCalendarYear = ctrl.siteCurrencyCalendar.calendarDay.year;
				ctrl.siteCurrencyCalendar.calendarDay.yearList = ctrl.siteCurrencyCalendar.calendarDay.yearList ||[];
				if ( ctrl.siteCurrencyCalendar.calendarDay.yearList.length == 0) {	
					ctrl.siteCurrencyCalendar.calendarDay.yearList.push({year: ctrl.siteCurrencyCalendar.calendarDay.year});
				}
				$scope.yearList = ctrl.siteCurrencyCalendar.calendarDay.yearList;
			}, angular.noop);
		};
	}
})(angular);